package com.koron.common.core.business.workflow.bean.properties;

import com.koron.bean.base.Response;
import com.koron.common.core.business.workflow.bean.param.BaseParam;
import com.koron.common.core.business.workflow.feign.ConfigureFeignService;
import com.koron.common.core.business.workflow.util.WorkflowConfigTool;
import com.koron.common.core.util.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 使用：
 * WorkflowProperties wfp = WorkflowProperties.init("processCode");
 * 之后就可以启动工作流了
 *
 * @author jzp
 * @since 2021/9/1
 */
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WorkflowProperties {
    private String appCode;
    private String setCode;
    private String processCode;
    private String moduleCode;
    private String callbackController;

    public BaseParam baseParam() {
        return new BaseParam(appCode, setCode, processCode);
    }

    /**
     * 平滑升级
     * 如果能从流程配置中拿到，则拿之
     * 否则从配置项中拿
     */
    public static WorkflowProperties init(String processCode) {
        // 1. 从流程配置中拿
        ConfigureFeignService configureFeignService = SpringUtils.getBean(ConfigureFeignService.class);
        Response<WorkflowProperties> response = configureFeignService.getWorkflowProperties(processCode);
        if (response != null && response.getData() != null) {
            return response.getData();
        }

        // @TODO 后期应该删掉配置项中所有的工作流配置，将之迁移到业务配置管理模块的流程配置中
        // 2. 从配置项管理里面获取工作流配置
        WorkflowConfigTool workflowConfigTool = SpringUtils.getBean(WorkflowConfigTool.class);
        return workflowConfigTool.getConfigByProcessCode(processCode);
    }

}
